package com.changgou.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author kfzhao
 * @version 1.0
 * @className RabbitMQConfig
 * @description rabbitmq配置类
 * @date 2021/12/27 0:41
 */
@Configuration
public class RabbitMQConfig {

    // 定义交换机名称
    public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";
    public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";

    // 定义队列名称（接收商品消息）
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";

    // 定义队列名称
    public static final String SEARCH_ADD_QUEUE = "search_add_queue";
    public static final String SEARCH_DEL_QUEUE = "search_del_queue";

    // 定义队列名称 静态化页面生成
    public static final String PAGE_CREATE_QUEUE = "page_create_queue";


    // 声明队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue searchAddQueue() {
        return new Queue(SEARCH_ADD_QUEUE);
    }

    @Bean(SEARCH_DEL_QUEUE)
    public Queue searchDelQueue() {
        return new Queue(SEARCH_DEL_QUEUE);
    }

    @Bean(AD_UPDATE_QUEUE)
    public Queue adSearchQueue() {
        return new Queue(AD_UPDATE_QUEUE);
    }

    @Bean(PAGE_CREATE_QUEUE)
    public Queue pageCreateQueue() {
        return new Queue(PAGE_CREATE_QUEUE);
    }

    // 声明交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange goodsUpExchange() {
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange goodsDownExchange() {
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    // 队列与交换机的绑定
    @Bean
    public Binding goodsUpExchangeBinding(@Qualifier(AD_UPDATE_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("")
                .noargs();
    }

    @Bean
    public Binding goodsDownExchangeBinding(@Qualifier(SEARCH_DEL_QUEUE) Queue queue, @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("")
                .noargs();
    }

    @Bean
    public Binding pageCreateQueueBinding(@Qualifier(PAGE_CREATE_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("")
                .noargs();
    }
}
